<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Model\Hint;
use App\Http\Services\FinancePayServices;
use App\Http\Services\FinanceRechargeOrderServices;
use App\Http\Services\UserServices;
use App\Lib\Response;

class H5Controller extends BaseController
{
    protected $userService;//用户服务
    protected $payService;//支付服务
    protected $service;//充值订单服务
    protected $payconfig;//支付配置

    public function __construct(FinancePayServices $payService,FinanceRechargeOrderServices $service)
    {
        $this->userService = new UserServices(); //用户服务层
        $this->payService = $payService;
        $this->service = $service;
        $this->payconfig = config('pay');
    }
    /**
     * 根据用户UID查询用户信息
     */
    public function searchByUid(Request $request)
    {
        //用户UID
        $uid = $request->get('uid',0);
        if(empty($uid)){
            Response::returnResult(Hint::PARAM_ERROR,[],'参数错误',0);
        }
        //根据UID获取用户信息
        $userInfo = $this->userService->getUserInfoByUuid($uid,true);
        if(empty($userInfo)){
            Response::returnResult(Hint::PARAM_ERROR,[],'暂无此用户',0);
        }
        Response::returnResult(Hint::SUCCESS,$userInfo,'ok',0);
    }
    
    /**
     * 网页支付
     */
    public function payH5(Request $request)
    {
        $rule_id = (int) $request->post('rule_id');//规则id
        $pay_type = (int) $request->post('pay_type');//支付类型	1=支付宝 2=微信 3=苹果 6=支付宝h5 7=微信H5 8=公众号
        $plat_type = (int) $request->post('plat_type');//客户端类型 1=iOS 2=android 3=apple内购 4=公众号 5=后台充值 6=H5]
        $custom_price = (int) $request->post('custom_price') ?? 0; //自定义充值金额
        $rechargeType = (int) $request->post('recharge_type',0); //充值类型 0=钻石 
        $user_id = (int) $request->post('user_id',0); //充值用户id 
        $openid = $request->post('openid'); //微信openid 
        
        //openid
        if(empty($openid) && $pay_type == 8){
            Response::returnResult(Hint::PARAM_ERROR,[],'请重新授权~',0);
        }

        //支付宝问题
        if($pay_type == 1) {
            Response::returnResult(Hint::PARAM_ERROR, [], '支付宝充值升级，请使用微信充值');
        }

        //单比最多充值限制
        if($custom_price > 100000){
            Response::returnResult(Hint::FINANCE_RECHARGE_PAY_MAX,[],'单笔最多可充值10万元~',0);
        }
        //根据USERID获取用户信息
        $userInfo = $this->userService->getUserInfoByUuid($user_id,false);
        if(empty($userInfo)){
            Response::returnResult(Hint::USER_NOT_EXIST,[],'用户不存在',0);
        }

        //充值黑名单
        $payBlack = $this->service->checkUserInRechargeBlack($user_id, $pay_type);
        if($payBlack) {
            Response::returnResult(Hint::FINANCE_RECHARGE_BLACK,[],'',0);
        }
        if($pay_type == 6) {

        }
        //判断支付方式
        if(!in_array($pay_type, [6,7,8])){
            $pay_type = 0;
        }
        if ($rule_id === null || empty($pay_type) || $plat_type == null) {
            Response::returnResult(Hint::PARAM_ERROR,[],'',0);
        }
        //判断支付金额
        if(!empty($custom_price)){
            $rule_id = 0;
            if($custom_price < 6){
                Response::returnResult(Hint::PARAM_ERROR, [], '抱歉，自定义金额最低充值6元',0);
            }
        }
    
        //根据pay_type读取流水id，和支付配置名称
        $payConfig = $this->payService->getConfigByPayType($pay_type);
        if(empty($payConfig)){
            Response::returnResult(Hint::FINANCE_RECHARGE_TYPE_ERROR,[],'支付配置定义错误',0);
        }
        $createRes = $this->service->createRechargeOrder($userInfo['user_id'], $rule_id, $payConfig['stream_type'], $plat_type,$rechargeType,$custom_price);
        if(is_array($createRes) && !empty($createRes['orderId'])) {
            $order_id = $createRes['orderId'];
        }else{
            Response::returnResult(Hint::ERROR,[],'创建订单失败',0);
        }
        $order_info = $this->service->getRechargeOrder($order_id);
        if ($order_info == false || empty($order_info)) {
            Response::returnResult(Hint::ERROR,[],'创建订单失败',0);
        }
        $result = [];
        switch ($pay_type) {
            case 6://支付宝h5
                $result = $this->payService->orderAlipayH5($payConfig['config_name'],$order_info,'wap');
                break;
            case 7://微信h5
                //$result = $this->payService->unifiedOrderWxH5($payConfig['config_name'],$order_info);
                break;
            case 8://微信公众号
                $result = $this->payService->unifiedOrderWxMp($payConfig['config_name'],$order_info,$openid);
                break;
            default:
                break;
        }

        if (empty($result)) {
            Response::returnResult(Hint::ERROR,[],'重新尝试支付',0);
        }
            Response::returnResult(Hint::SUCCESS, $result,'ok',0);
    }
    
    //获取微信openID
    public function getWxOpenid(Request $request){
        $code = $request->get('code',0);
        if(empty($code)){
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //根据pay_type读取流水id，和支付配置名称
        $payConfig = $this->payService->getConfigByPayType(8);
        $res = $this->payService->getUserOpenId($payConfig['config_name'],$code);
        if(isset($res['errcode'])){
            Response::returnResult(Hint::ERROR, $res,'ok',0);
        }
        Response::returnResult(Hint::SUCCESS, $res,'ok',0);
    }

    /**
     * 获取微信授权参数
     */
    public function getWxOauthParam(Request $request)
    {
        $user_id = (int) $request->get('user_id',0); //充值用户id 
         //根据USERID获取用户信息
         $userInfo = $this->userService->getUserInfoByUuid($user_id,false);
         if(empty($userInfo)){
             Response::returnResult(Hint::USER_NOT_EXIST,[],'用户不存在',0);
         }
         //获取支付配置
         $payConfig = $this->payconfig;
         $data['app_id'] = $payConfig['pay_wx_h5']['app_id'];
         $data['response_type'] = 'code';
         $data['scope'] = 'snsapi_base';
         Response::returnResult(Hint::SUCCESS, $data,'ok',0);
    }
}